<!DOCTYPE html>
<html lang="en">
<include file="CommonSidebar:common-head"/>
<link href="__ROOT__/Application/Admin/Public/css/jquery.datetimepicker.css" rel="stylesheet">
<body>
<link href="__ROOT__/Application/Admin/Public/css/cropper/cropper.min.css" rel="stylesheet">
<link href="__ROOT__/Application/Admin/Public/css/main.css" rel="stylesheet">
<js href="__ROOT__/Application/Admin/Public/ueditor/ueditor.config.js"/>
<js href="__ROOT__/Application/Admin/Public/ueditor/ueditor.all.min.js"/>
<js href="__ROOT__/Application/Admin/Public/js/jquery-2.0.2.js"/>

<!--编辑器-->
<js href="__ROOT__/Application/Admin/Public/ueditor/ueditor.config.js"/>
<js href="__ROOT__/Application/Admin/Public/ueditor/ueditor.all.min.js"/>
<script>
    $(function () {
        var ue = UE.getEditor('container');
    });
</script>

<div id="wrapper">

    <!-- Navigation -->
    <include file="CommonSidebar:common-navbar"/>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">添加/编辑书籍</h1>
            </div>
        </div>

        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                            <div class="panel panel-default">
                                <div class="panel-heading" role="tab" id="headingOne">
                                    <h4 class="panel-title">
                                        <a role="button" data-toggle="collapse" data-parent="#accordion"
                                           href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                            添加/编辑书籍
                                        </a>
                                    </h4>
                                </div>
                                <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel"
                                     aria-labelledby="headingOne">
                                    <div class="panel-body">
                                        <form id="editStoreForm" action="{:U('EditBook')}?BHCODE={$BHCODE}"
                                              class="form-horizontal"
                                              method="post">

                                            <input id="BOOK_ID" name="ID" type="hidden" value="{$DATA['ID']}">

                                            <div class="form-group">

                                                <label class="col-sm-2 control-label">商品主图:</label>

                                                <div class="col-sm-10">
                                                    <div class="goods-img-list">
                                                        <div class="goods-img-item">
                                                            <img style="height: 250px;"
                                                                 src="{$DATA['PIC_URL']}" alt="">
                                                            <span class="glyphicon close-goods-img glyphicon-remove-circle"></span>
                                                        </div>

                                                        <div class="goods-img-item">
                                                            <img height="100px" class="add-goods-img avatar-view"
                                                                 src="__ROOT__/Application/Admin/Public/images/default.png"
                                                                 alt=""
                                                                 style="margin-left:20px">
                                                        </div>

                                                    </div>
                                                    <p class="help-block" style="display: none">上传图片</p>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">一级分类:</label>

                                                <div class="col-sm-3">
                                                    <select id="status" type="text" class="form-control"
                                                            name="region_one">
                                                        <option value="{$DATA['region_one']}">{$DATA['region_one']}
                                                        </option>
                                                        <foreach name="yjfldata" item="v" key="k">
                                                            <option value="{$v.region_id}">{$v.region_name}</option>
                                                        </foreach>
                                                    </select>
                                                </div>
                                                <label class="col-sm-2 control-label">二级分类:</label>

                                                <div class="col-sm-3">

                                                    <select id="two" type="text" class="form-control" name="region_two">
                                                        <option value="{$DATA['region_two']}">{$DATA['region_two']}
                                                        </option>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">书籍名称:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="TITLE"
                                                           value="{$DATA['TITLE']}">
                                                </div>
                                                <label class="col-sm-2 control-label">条形码ISBN:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="ISBN"
                                                           value="{$DATA['ISBN']}">
                                                </div>
                                            </div>
                                            <div class="form-group">

                                                <label class="col-sm-2 control-label">作者:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="AUTHOR"
                                                           value="{$DATA['AUTHOR']}">
                                                </div>

                                                <label class="col-sm-2 control-label">出版社:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="PRESS"
                                                           value="{$DATA['PRESS']}">
                                                </div>

                                            </div>
                                            <div class="form-group">

                                                <label class="col-sm-2 control-label">出版日期:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="PUBLICATION_DATE"
                                                           value="{$DATA['PUBLICATION_DATE']}">
                                                </div>

                                                <label class="col-sm-2 control-label">包装规格:</label>

                                                <div class="col-sm-3">

                                                    <select type="text" class="form-control"
                                                            name="PACKING_SPECIFICATION">
                                                        <option value="{$DATA['PACKING_SPECIFICATION']}">
                                                            {$DATA['PACKING_SPECIFICATION']}
                                                        </option>
                                                        <foreach name="BOOK_PACKING" item="v" key="k">
                                                            <option value="{$v.region_name}">{$v.region_name}</option>
                                                        </foreach>
                                                    </select>
                                                </div>

                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">阅读年龄:</label>

                                                <div class="col-sm-3">
                                                    <input class="form-control" name="AGE" value="{$DATA.AGE}"
                                                           readonly="readonly" id="AGE" onclick="AGECHANGE($(this))">
                                                    <ul class="list-group" hidden>
                                                        <foreach name="BOOK_AGE" item="v" key="k">
                                                            <li class="list-group-item">
                                                                <input onclick="SelectAge()" type="checkbox"
                                                                       name="checkbox" value="{$v.region_name}">
                                                                {$v.region_name}
                                                            </li>
                                                        </foreach>
                                                    </ul>
                                                    </ul>
                                                </div>


                                                <label class="col-sm-2 control-label">库存:</label>

                                                <div class="col-sm-3">
                                                    <input type="number" class="form-control" name="STOCK" min="0"
                                                           id="STOCK" readonly="readonly">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">原价:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" id="Y_PRICE" name="Y_PRICE"
                                                           value="{$DATA['Y_PRICE']}">
                                                </div>

                                                <label class="col-sm-2 control-label">销售价格:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="XS_PRICE"
                                                           value="{$DATA['XS_PRICE']}" readonly="readonly">
                                                </div>
                                            </div>
                                            <!--<div class="form-group">-->
                                            <!--<label class="col-sm-2 control-label">书籍新旧水平:</label>-->

                                            <!--<div class="col-sm-3">-->
                                            <!--<select type="text" class="form-control" name="LEVEL">-->
                                            <!--<option value="{$DATA['LEVEL']}">{$DATA['LEVEL']}</option>-->
                                            <!--<foreach name="BOOK_LEVEL" item="v" key="k">-->
                                            <!--<option value="{$v.region_name}">{$v.region_name}</option>-->
                                            <!--</foreach>-->
                                            <!--</select>-->

                                            <!--</div>-->

                                            <!--<label class="col-sm-2 control-label">借阅次数:</label>-->

                                            <!--<div class="col-sm-3">-->
                                            <!--<input type="number" class="form-control" name="BORROW_NUM"-->
                                            <!--value="{$DATA['BORROW_NUM']}" readonly="readonly">-->
                                            <!--</div>-->
                                            <!--</div>-->
                                            <div class="form-group">


                                                <label class="col-sm-2 control-label">语言:</label>

                                                <div class="col-sm-3">
                                                    <select type="text" class="form-control" name="LANGUAGES">
                                                        <option value="{$DATA['LANGUAGES']}">{$DATA['LANGUAGES']}
                                                        </option>
                                                        <foreach name="BOOK_LANGUAGES" item="v" key="k">
                                                            <option value="{$v.region_name}">{$v.region_name}</option>
                                                        </foreach>
                                                    </select>
                                                </div>

                                                <label class="col-sm-2 control-label">书籍状态:</label>

                                                <div class="col-sm-3">
                                                    <select type="text" class="form-control" name="STATE">
                                                        <option value="{$DATA['STATE']}">{$DATA['STATE']}</option>
                                                        <foreach name="BOOK_STATE" item="v" key="k">
                                                            <option value="{$v}">{$v}</option>
                                                        </foreach>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">音频链接:</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="BOOK_VIDEO"
                                                           value="{$DATA['BOOK_VIDEO']}">
                                                </div>
                                                <label class="col-sm-2 control-label">音频链接时长(秒):</label>

                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" name="BOOK_VIDEO_TIME"
                                                           value="{$DATA['BOOK_VIDEO_TIME']}">
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">书籍模糊搜索关键词语句（暂时保留）:</label>

                                                <div class="col-sm-8">
                                                    <input type="text" onclick="BQ_CHANGE()" class="form-control"
                                                           id="KEY" name="KEY"
                                                           value="{$DATA['KEY']}" readonly="readonly">
                                                </div>

                                            </div>


                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">书籍简介:</label>

                                                <div class="col-sm-8">
                                                    <textarea type="text" class="form-control" name="INFO" rows="4">{$DATA['INFO']}</textarea>
                                                </div>
                                            </div>
                                            <div class="form-group">

                                                <label class="col-sm-2 control-label">书籍详情:</label>

                                                <div class="col-sm-8">
                                                    <script id="container" id="CONTENT" name="CONTENT"
                                                            type="text/plain" required>{$DATA.details}

























































                                                    </script>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <div class="col-sm-2 col-sm-offset-2">
                                                    <button type="submit" class="btn btn-block btn-primary">保存</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>
    <!-- /#page-wrapper -->

</div>

<!--选择仓库位置-->
<div id="cityModal" class="modal fade bs-example-modal-sm" data-backdrop='static' tabindex="-1" role="dialog"
     aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true" style="font-size:35px">&times;</span></button>
                <h3 class="modal-title text-center">选择仓库位置</h3>

                <div class="row col-xs-4">
                    <span class="btn btn-primary " onclick="mrwz()">默认位置</span>
                </div>
                <div class="row col-xs-10">
                    <span class=" form-control">{$MRWZDATA.P}{$MRWZDATA.C}{$MRWZDATA.Z}{$MRWZDATA.ADDRESS}{$MRWZDATA.NAME}---{$MRWZDATA.SJCODE}---{$MRWZDATA.SGCODE}</span>
                </div>
                <input type="hidden" id="LEVEL">
            </div>
            <div class="modal-body" style="padding-top:0px;padding-bottom:5px;">
                <div class="row" id="city-content" style="height:530px;overflow-y:scroll;">
                    <div class="col-xs-4 spell-list"
                         style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">
                        <div class="row">
                            <foreach name="CKDATA" item="v" key="k">
                                <div class="col-xs-12 type-item " data-type="{$v.BHCODE}" data-align="sj">
                                    <h5>
                                        <span>{$v.NAME}</span>
                                        <span class="glyphicon glyphicon-menu-right" style="float:right;"></span>
                                    </h5>
                                </div>
                            </foreach>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--选择仓库位置-->

<!--选择标签-->
<div id="BOOK_BQ" class="modal fade bs-example-modal-sm" data-backdrop='static' tabindex="-1" role="dialog"
     aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true" style="font-size:35px">&times;</span></button>
                <h3 class="modal-title text-center">选择标签</h3>
            </div>
            <div>
                <button onclick="cleanBQ()" class="btn btn-block btn-primary" style="width:100px;">清除</button>
            </div>
            <div class="modal-body" style="padding-top:0px;padding-bottom:5px;">
                <div class="row" style="height:530px;overflow-y:scroll;">
                    <div class="col-xs-4 spell-list"
                         style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">
                        <div class="row">
                            <foreach name="BOOK_BQ" item="v" key="k">
                                <div class="col-xs-12 type-item-bq " data-type="{$v.region_id}">
                                    <h5>
                                        <span>{$v.region_name}</span>
                                        <span class="glyphicon glyphicon-menu-right" style="float:right;"></span>
                                    </h5>
                                </div>
                            </foreach>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!--选择标签-->


<!--查看位置-->
<div id="WZModal" class="modal fade bs-example-modal-sm" data-backdrop='static' tabindex="-1" role="dialog"
     aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true" style="font-size:35px">&times;</span></button>
                <h3 class="modal-title text-center">书籍位置</h3>
            </div>
            <div class="modal-body" style="padding-top:0px;padding-bottom:5px;">
                <div class="row" style="height:530px;overflow-y:scroll;">
                    <div class=" spell-list"
                         style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="panel-body">
                                    <div class="dataTable_wrapper">
                                        <table class="table table-striped table-bordered table-hover"
                                               id="dataTables-example">
                                            <thead>
                                            <tr>
                                                <th style="text-align: center">仓库</th>
                                                <th style="text-align:center">书架</th>
                                                <th style="text-align:center">书柜</th>
                                                <th style="text-align:center">移除</th>
                                                <th style="text-align:center">添加副本</th>
                                            </tr>
                                            </thead>
                                            <tbody id="WZDATA">

                                            </tbody>
                                        </table>
                                    </div>
                                    <!-- /.table-responsive -->
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--查看位置-->

<!--添加副本-->
<div id="AddFB" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog"
     aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title text-center">添加副本</h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <input id="WZID" type="hidden" name="WZID" value="">

                        <div class="col-xs-12">
                            <label>请输入添加本书</label>
                            <input id="book_num" name="book_num" class="rugby-login form-control" value="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button onclick="AddFBhandle()">
                    确认
                </button>
            </div>
        </div>
    </div>
</div>
<!--添加副本-->


<include file="CommonSidebar:common-script"/>
<!-- Custom Theme JavaScript -->
<script src="__ROOT__/Application/Admin/Public/dist/js/sb-admin-2.js"></script>
<script type="text/javascript" src="__ROOT__/Application/Admin/Public/js/uploadPreview.js"></script>
<script type="text/javascript" src="__ROOT__/Application/Admin/Public/js/jquery.form.min.js"></script>
<script type="text/javascript" src="__ROOT__/Application/Admin/Public/js/jquery.qrcode.min.js"></script>


<include file="Common:common-cropper"/>
<script src="__ROOT__/Application/Admin/Public/dist/js/sb-admin-2.js"></script>
<script type="text/javascript" src="__ROOT__/Application/Admin/Public/js/bootstrapValidator.js"></script>
<script src="__ROOT__/Application/Admin/Public/js/cropper/cropper.min.js"></script>
<script src="__ROOT__/Application/Admin/Public/js/figure.js"></script>


</body>
<script>
    $("#Y_PRICE").change(function () {
        $(this).css("background-color", "#FFFFCC");
        var Y_price = $(this).val();
        var BZ = jQuery.trim($("select[name='PACKING_SPECIFICATION']").val());
        if (BZ == '精装' || BZ == '精装-套装') {
            $("input[name='GG_PRICE1']").val(Y_price * 1);
            $("input[name='GG_PRICE2']").val(Y_price * 0.5);
            $("input[name='GG_PRICE3']").val(Y_price * 0.45);
            $("input[name='GG_PRICE4']").val(Y_price * 0.4);
            $("input[name='GG_PRICE5']").val(Y_price * 0.3);
        } else if (BZ == '平装' || BZ == '平装-套装') {
            $("input[name='GG_PRICE1']").val(Y_price * 1);
            $("input[name='GG_PRICE2']").val(Y_price * 0.4);
            $("input[name='GG_PRICE3']").val(Y_price * 0.35);
            $("input[name='GG_PRICE4']").val(Y_price * 0.3);
            $("input[name='GG_PRICE5']").val(Y_price * 0.2);
        } else {
            alert("请先维护包装");
        }
    });

    //显示隐藏年龄段
    function BQ_CHANGE(elem) {
        $("#BOOK_BQ").modal("show");
    }


    //显示隐藏年龄段
    function AGECHANGE(elem) {
        if (elem.next().is(":hidden")) {
            elem.next().show();
        } else {
            elem.next().hide();
        }
    }
    //选择年龄
    function SelectAge() {
        var vals = '';
        $("input[name='checkbox']:checkbox:checked").each(function (index, item) {
            vals += $(this).val();
            vals += ',';
        });
        $("#AGE").val(vals);
    }

    //    移除位置
    function ycwz(ID, elem) {
        if (confirm("确认移除？")) {
            $.post("{:U('ycwz')}",
                    {ID: ID},
                    function (data) {
                        console.log(data);
                        if (data == 1) {
                            alert('移除成功');
                            elem.parent().parent().remove();
                        } else {
                            alert('移除失败');
                        }
                    });
        }

    }

    //    查看书籍位置
    function ckwz(ID) {
        $.post("{:U('GetBookWZ')}",
                {ID: ID},
                function (data) {
                    console.log(data);
                    var html = '';
                    $("#WZDATA").html("");
                    for (var i = 0; i < data.length; i++) {
                        html += "<tr class='odd gradeX' > " +
                                "<td class='enter' style='text-align:center'> "
                                + data[i]['P']
                                + data[i]['C']
                                + data[i]['Z']
                                + data[i]['ADDRESS']
                                + data[i]['NAME'] +
                                "</td> " +
                                "<td class='center' style='text-align:center'> "
                                + data[i]['SJCODE'] +
                                "</td> " +
                                "<td class='center' style='text-align:center'> "
                                + data[i]['SGCODE'] +
                                "</td> " +
                                "<td class='center' style='text-align:center'><a onclick='ycwz(" + data[i]['BOOK_WZ_ID'] + ",$(this))'> 移除</a></td> " +
                                "<td width='20px'onclick='addfb(" + data[i]['BOOK_WZ_ID'] + ",$(this));'> " +
                                "<img height='30px' class='deleteNorms' src='/Book/Application/Admin/Public/images/default.png'> " +
                                "</td>" +
                                "</tr>";
                    }
                    $("#WZDATA").append(html);

                    $("#WZModal").modal("show");
                });
    }
</script>
<script>
    //添加副本弹窗
    function addfb(WZID, elem) {
        $("#WZID").val(WZID);
        $("#AddFB").modal('show');
    }
    //添加副本处理事件
    function AddFBhandle() {
        var NUM = $('#book_num').val();
        var WZID = $('#WZID').val();
        if (confirm("是否确认添加??")) {
            $.post("{:U('AddFBhandle')}",
                    {WZID: WZID, NUM: NUM},
                    function (data1) {
                        alert('成功添加位置数：' + data1);
                        window.location.reload();
                    });
        }

    }

    var bqdatas = getbq({$BQDATA_JSON});//初始化标签信息
    var allbq = bqdatas[0];//所有标签
    var allbq_id = bqdatas[1];//所有标签id
    var fbq = '';//临时父亲标签、
    var fbq_id = '';//临时父亲标签id

    //添加规格
    function addGG(LEVEL) {
        $('#cityModal').modal("show");
        $('#LEVEL').val(LEVEL);
    }
    //点击默认
    function mrwz() {
        var BOOK_ID = $("#BOOK_ID").val();
        var LEVEL = $('#LEVEL').val();
        var ID = '{$MRWZDATA.WZID}';
        if (ID) {
            $.post("{:U('StoreHouse/Select')}",
                    {ID: ID, name: 'wz', BOOK_ID: BOOK_ID, LEVEL: LEVEL},
                    function (data1) {
                        if (data1['name'] == '1' && data1['OK']) {
                            alert('添加成功');
                            window.location.reload();
                            $('#cityModal').modal("hide");

                        }
                    });
        } else {
            alert('请先设置默认位置，再进行入库操作');
        }

    }

    $(document).on("click", ".type-item", function () {
        var BOOK_ID = $("#BOOK_ID").val();
        var $elem = $(this);
        var ID = $elem.attr("data-type");
        var name = $elem.attr("data-align");
        var LEVEL = $('#LEVEL').val();
        $.post("{:U('StoreHouse/Select')}",
                {ID: ID, name: name, BOOK_ID: BOOK_ID, LEVEL: LEVEL},
                function (data1) {
                    console.log(data1);
                    if (data1['datas'].length > 0) {
                        var data = data1['datas'];
                        $elem.siblings().css('color', '#777');
                        $elem.css('color', '#000079');
                        var $cities = $elem.parent().parent();
                        $elem.parent().parent().nextAll().remove();
                        /*active*/
                        var html = '';
                        html += ' <div class="col-xs-4 spell-list" style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">' +
                                ' <div class="row">';
                        for (var i = 0; i < data.length; i++) {
                            html += '<div class="col-xs-12 type-item " data-type="' + data[i]['BHCODE'] +
                                    '" data-align="' + data1['name'] + '">' +
                                    '<h5>' +
                                    '<span>' + data[i]['CODE'] + '</span>' +
                                    '<span  class="glyphicon glyphicon-menu-right" style="float:right;"></span>' +
                                    '</h5>' +
                                    '</div>';
                        }
                        html += '</div>' +
                                '</div>';
                        $cities.after(html);
                    } else {
                        if (data1['name'] == '1' && data1['OK']) {
                            alert('添加成功');
                            window.location.reload()
                        }
                        $('#cityModal').modal("hide");

                    }


                });
    });

    //标签

    //获取标签初始化数据
    function getbq(data) {
        var bqstr = [];
        var bqid = [];
        for (var i = 0; i < data.length; i++) {
            bqstr.push(data[i]['region_name']);
            bqid.push(data[i]['region_id']);
        }

        var res = [];
        res[0] = bqstr;
        res[1] = bqid;
        return res;
    }

    //生成标签HTML
    function bqHTML(allbqs, allbq_ids) {
        var bqhtml = '';
        var showbq = '';
        for (var i = 0; i < allbqs.length; i++) {
            bqhtml += '<h4><span  class="label label-info" data-type="' + allbq_ids[i] + '" >' + allbqs[i] + '</span><h4/>';
            showbq += '<nobr><span style="font-size:15px"  class="label label-info form-control" data-type="' + allbq_ids[i] + '" >' + allbqs[i] + '  </span></nobr>';
        }
        var res = [];
        res[0] = bqhtml;
        res[1] = showbq;
        return res;
    }


    $(document).on("click", ".type-item-bq", function () {
        var $elem = $(this);
        var ID = $elem.attr("data-type");

        $.post("{:U('Book/SelectQB')}",
                {ID: ID},
                function (data) {
                    console.log(data);
                    if (data.length > 0) {
                        $elem.siblings().css('color', '#777');
                        $elem.css('color', '#000079');
                        var $cities = $elem.parent().parent();
                        $elem.parent().parent().nextAll().remove();
                        /*active*/
                        var html = '';
                        html += ' <div class="col-xs-4 spell-list" style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">' +
                                ' <div class="row">';
                        for (var i = 0; i < data.length; i++) {
                            html += '<div class="col-xs-12 type-item-bq " data-type="' + data[i]['region_id'] +
                                    '" data-align="' + data['region_name'] + '">' +
                                    '<h5>' +
                                    '<span>' + data[i]['region_name'] + '</span>' +
                                    '</h5>' +
                                    '</div>';
                        }
                        html += '<div class="col-xs-12 type-item-bq ">' +
                                '<h5>' +
                                '<span>查看</span>' +
                                '</h5>' +
                                '</div>';
                        html += '</div>' +
                                '</div>';
                        $cities.after(html);
                        fbq = $elem.find('span').html();
                        fbq_id = ID;
                    } else {
                        var sonbq = $elem.find('span').html();
                        var index = $.inArray(sonbq, allbq);   //结果：index=1
                        if (index < 0 && sonbq != '查看') {
                            allbq.push(sonbq);
                        }
                        //父亲标签是否存在
                        if ($.inArray(fbq, allbq) < 0 && sonbq != '查看') {
                            allbq.push(fbq);
                        }
//                        console.log(allbq);

                        //标签id
                        if ($.inArray(ID, allbq_id) < 0 && sonbq != '查看') {
                            allbq_id.push(ID);
                        }
                        //父亲标签id
                        if ($.inArray(fbq_id, allbq_id) < 0 && sonbq != '查看') {
                            allbq_id.push(fbq_id);
                        }
                        console.log(allbq_id);

                        var book_id = $("#BOOK_ID").val();
                        $.post("{:U('Book/addbq')}",
                                {'allbq_ids': allbq_id, 'book_id': book_id},
                                function (data) {

                                    console.log(data);
                                });


                        var bqhtml = bqHTML(allbq, allbq_id)[0];
                        var showbq = bqHTML(allbq, allbq_id)[1];
                        $('#book_all_bq').parent().find('nobr').remove();
                        $('#book_all_bq').append(showbq);

                        var html = '';
                        html += ' <div class="col-xs-4 spell-list" style="height:100%; background:#f0f0f0; overflow-y:scroll; overflow-x:hidden; -webkit-overflow-scrolling:touch">' +
                                ' <div class="row">';
                        html += '<div class="col-xs-12">' +
                                bqhtml +
                                '</div>';
                        html += '</div>' +
                                '</div>';
                        $elem.parent().parent().nextAll().remove();
                        $elem.parent().parent().after(html);
                        $('#KEY').val(allbq);
                        $elem.remove();

                    }


                });
    });

    //删除单个标签
    $(document).on("click", ".label-info", function () {
        var valuestr = jQuery.trim($(this).html());
        var valueid = jQuery.trim($(this).attr("data-type"));
        var BOOK_ID = $("#BOOK_ID").val();

        var elem = $(this);

//        alert(valueid);
        if (confirm("确认删除标签《 " + valuestr + " 》")) {
            $.post("{:U('DelBq')}",
                    {BQ_ID: valueid, BOOK_ID: BOOK_ID},
                    function (OK) {
                        if (OK == 1) {
                            elem.parent().remove();
                            allbq.splice($.inArray(valuestr, allbq), 1);
                            allbq_id.splice($.inArray(valueid, allbq_id), 1);
                            var showbq = bqHTML(allbq, allbq_id)[1];
                            $('#book_all_bq').parent().parent().find('nobr').remove();
                            $('#book_all_bq').append(showbq);
                            $('#KEY').val(allbq);
                            alert('删除成功');
                        } else {
                            alert('删除失败');
                        }

                    });

        }
    });

    //清除标签
    function cleanBQ() {
        allbq = [];
        $("#KEY").val('');
        alert('清除成功');
    }


    $("select#status").change(function () {
        var ID = $(this).val();
        $.post("{:U('GetRegion_two')}",
                {ID: ID},
                function (data) {
                    $("#two").empty();
                    $.each(data, function (n, value) {
                        $("#two").append("<option value='" + value.region_name + "'>" + value.region_name + "</option>");
                    });

                });
    });
</script>
<script>
    $(function () {
        new CropAvatar({
            container: '#crop-avatar',
            workspaces: '__ROOT',
            formUrl: "{:U('Pics/Pics')}",
            submitDone: function (data) {
                console.log(data);
                var $avatarView = $('.avatar-view');
                if (data.state == 1) {
                    var imgUrl = data.picpath;
                    var imgUrlTH = data.data;
                    var html = ' <div class="goods-img-item">';
                    html += '<img style="height: 250px" src="' + '__ROOT__/' + imgUrl + '" alt="">';
                    html += '<span class="glyphicon close-goods-img glyphicon-remove-circle"></span>';
                    html += '<input type="hidden" value="' + imgUrlTH + '" name="thumbpicpath[]" >';
                    html += ' <input type="hidden" value="' + imgUrl + '" name="picpath[]" >';
                    html += '</div>';
                    $avatarView.before(html);
                    $avatarView.hide();
                } else {
                    alert('Failed to response');
                }
            },
            delete: function (e) {
                var _this = e.target;
                var flag = _this.className.indexOf('close-goods-img');
                var $avatarView = $('.avatar-view');
                if (flag >= 0) {
                    var $elem = $(_this);
                    if (confirm("确认移除？")) {
                        $avatarView.show();
                        $elem.parent(".goods-img-item").remove();
                        $avatarView.next("input[name='thumbpicpath']").val("");
                        $avatarView.next("input[name='picpath']").val("");
                        $('.add-goods-img').attr('src', options.workspaces + '/Public/Boss/images/default.png')
                    }
                }
            }

        });

        var num1 = parseInt($('#GG_PRICE1_KC').val());
        var num2 = parseInt($('#GG_PRICE2_KC').val());
        var num3 = parseInt($('#GG_PRICE3_KC').val());
        var num4 = parseInt($('#GG_PRICE4_KC').val());
        var num5 = parseInt($('#GG_PRICE5_KC').val());
        if (isNaN(num1)) num1 = parseInt(0);
        if (isNaN(num2)) num2 = parseInt(0);
        if (isNaN(num3)) num3 = parseInt(0);
        if (isNaN(num4)) num4 = parseInt(0);
        if (isNaN(num5)) num5 = parseInt(0);
        var num = num1 + num2 + num3 + num4 + num5;
        $("#STOCK").val(num);
    });

    function submitcheck() {
        var $picid = $("input[name='picid']");
        if ($picid.val() == "") {
            $picid.parents(".form-group").removeClass("has-success").addClass("has-error");
            $picid.parents(".form-group").find(".help-block").show();
            return false;
        }
    }
</script>
</html>
